package com.youlai.boot.factory.controller;

import com.youlai.boot.auth.model.dto.WxMiniAppCodeLoginDTO;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.core.security.model.AuthenticationToken;
import com.youlai.boot.factory.model.entity.FactoryMember;
import com.youlai.boot.factory.service.FactoryMemberService;
import com.youlai.boot.system.model.dto.CurrentUserDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.FactoryMemberForm;
import com.youlai.boot.factory.model.query.FactoryMemberQuery;
import com.youlai.boot.factory.model.vo.FactoryMemberVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 服装厂员工信息前端控制层
 *
 * @author youlaitech
 * @since 2025-09-28 21:51
 */
@Tag(name = "服装厂员工信息接口")
@RestController
@RequestMapping("/api/v1/factory-member")
@RequiredArgsConstructor
public class FactoryMemberController  {

    private final FactoryMemberService factoryMemberService;

    @Operation(summary = "服装厂员工信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:factory-member:query')")
    public PageResult<FactoryMemberVO> getFactoryMemberPage(FactoryMemberQuery queryParams ) {
        IPage<FactoryMemberVO> result = factoryMemberService.getFactoryMemberPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增服装厂员工信息")
    @PostMapping("register")
    @PreAuthorize("@ss.hasPerm('factory:factory-member:add')")
    public Result<Void> saveFactoryMember(@RequestBody @Valid FactoryMemberForm formData ) {
        boolean result = factoryMemberService.saveFactoryMember(formData);
        return Result.judge(result);
    }


    @Operation(summary = "获取服装厂员工信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:factory-member:edit')")
    public Result<FactoryMemberForm> getFactoryMemberForm(
        @Parameter(description = "服装厂员工信息ID") @PathVariable Long id
    ) {
        FactoryMemberForm formData = factoryMemberService.getFactoryMemberFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改服装厂员工信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:factory-member:edit')")
    public Result<Void> updateFactoryMember(
            @Parameter(description = "服装厂员工信息ID") @PathVariable Long id,
            @RequestBody @Validated FactoryMemberForm formData
    ) {
        boolean result = factoryMemberService.updateFactoryMember(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除服装厂员工信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:factory-member:delete')")
    public Result<Void> deleteFactoryMembers(
        @Parameter(description = "服装厂员工信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = factoryMemberService.deleteFactoryMembers(ids);
        return Result.judge(result);
    }


    @Operation(summary = "获取当前登录外部用户信息")
    @GetMapping("/me")
    public Result<FactoryMember> getCurrentUser() {
        FactoryMember factoryMember = factoryMemberService.getCurrentUserInfo();
        return Result.success(factoryMember);
    }

}
